package com.example.apidemo2;

import java.io.File;
import java.io.RandomAccessFile;

import android.app.Activity;
import android.content.Context;

import com.pos.utils.SDFile;

public class SDCardTest {
	
	private static String strReadContent = null;
	private static String strWriteContent = null;

	public static void SDCardReadWriteTest(final Activity activity)
	{
		SysActivity.SendPromptMsg("Start SD Card Test...\n");
		String path = SDFile.getSecondaryStoragePath(activity);
		
		if(path == null){
			path = "";
		}
		
		File dirTrsmCertFiles = new File(path);
		if (!dirTrsmCertFiles.exists()) {

			SysActivity.SendPromptMsg("SD Card is not exist!!!\r\n"); 
			return ;
		}
		
        RandomAccessFile newfile = null;
        byte[] byFileBuff = new byte[1024];
        int iFileLen = -1;
        
		try {
			String strFilePath = SDFile.getSecondaryStoragePath(activity) + "/test.txt";
			strWriteContent="Production Test";
			newfile = new RandomAccessFile(strFilePath, "rw");
	        newfile.write(strWriteContent.getBytes());
	        SysActivity.SendPromptMsg("Write file data: "+strWriteContent+"\n");
	        newfile.close();

	        newfile = new RandomAccessFile(strFilePath, "r");
	        iFileLen = (int) newfile.length();
            if(iFileLen > 0)
            {
            	 newfile.read(byFileBuff, 0, iFileLen);
            	 strReadContent=new String(byFileBuff).trim();
            }
            
            newfile.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(iFileLen <= 0)
		{
			SysActivity.SendPromptMsg("SD Card Test failed\n");
			return;
		}
		
	//	String strFileContent = "File Content:" + new String(byFileBuff).trim();
	//	MainActivity.sendResultPromptMsg(strFileContent + "\n");
		
	//	MainActivity.sendResultPromptMsg("Finish SD Card Test, please confirm SD card file\n");
		SysActivity.SendPromptMsg("Read file data: "+strReadContent+"\n");
		
      if (strReadContent.equals(strWriteContent)) {
			
    	  SysActivity.SendPromptMsg("Test write and read file pass");
		}
     else
     {
    	 SysActivity.SendPromptMsg("Test write and read file failed");
     }
	}
	
	public static void EndSDCardReadWriteTest(Context context)
	{
		if(SDFile.getSecondaryStoragePath(context) == null){
			return;
		}
		String strFilePath = SDFile.getSecondaryStoragePath(context)  + "/test.txt";
		
		SDFile.deleteFile(strFilePath);
	}

}
